﻿using System;
using System.Data;
using Titan.FIS.Enum;

namespace Titan.FIS.LMS.DataOperate
{
    public class DataExecProc
    {
        /// <summary>
        /// 插入平行样计算结果到确认表
        /// </summary>
        /// <param name="strTestCode"></param>
        /// <param name="iTestIndex"></param>
        /// <param name="strTestValue"></param>
        /// <param name="iTestMode"></param>
        /// <param name="iCType"></param>
        /// <param name="iSType"></param>
        /// <param name="strErrInfo"></param>
        /// <returns></returns>
        public static bool DoInsertConfirmedData(string strTestCode, int iTestIndex, string strTestValue, int iLab, int iTestMode, int iCType, int iSType, ref string strErrInfo)
        {
            var bResult = false;
            strErrInfo = string.Empty;
            var parms = new[]{    new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTCODE",Value = strTestCode,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTINDEX",Value = iTestIndex,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTVALUE",Value = strTestValue,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_LABORATORY",Value = iLab,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTMODE",Value = iTestMode,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_STYPE",Value = iSType,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_CTYPE",Value = iCType,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_USERID",Value = Common.Common.UserInfo.UserId,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRCODE",Value = 0,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRMSG",Value = null,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2),Size=2000}};
            var modelProc = DAS.Client.Data.DataHelper.Instance.ExecProc(Common.Common.UserInfo.UserName, Common.Common.UserInfo.UserPwd, "PCK_LMSDATAINFO.INSERT_LMS_CONFIRMEDDATA", parms);
            if (!modelProc.RSuccess || modelProc.RStatuCode != 1)
            {
                strErrInfo = string.Format("操作失败!\r\n错误代码:{0}  错误描述:{1}", modelProc.RStatuCode, modelProc.RStatuDesc);
            }
            else
            {
                strErrInfo = modelProc.RParams[modelProc.RParams.Length - 1].Value == null ? string.Empty : modelProc.RParams[modelProc.RParams.Length - 1].Value.ToString().Replace("null", "").Trim();
                if (!string.IsNullOrEmpty(strErrInfo)) return false;
                bResult = true;
            }
            return bResult;
        }


        /// <summary>
        /// 从确认表删除审核未通过的数据，包括复检数据
        /// </summary>
        /// <param name="strTestCode"></param>
        /// <param name="strTestIndexs"></param>
        /// <param name="strUpdateTime"></param>
        /// <param name="strErrInfo"></param>
        /// <returns></returns>
        public static bool DoDeleteConfirmedData(string strTestCode, string strTestIndexs, string strUpdateTime, ref string strErrInfo)
        {
            var bResult = false;
            strErrInfo = string.Empty;
            var parms = new[]{                 new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTCODE",Value = strTestCode,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTINDEXS",Value = strTestIndexs,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_UPDATETIME",Value = strUpdateTime,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_TYPE",Value = 1,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRCODE",Value = 0,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRMSG",Value = null,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2),Size=2000}};
            var modelProc = DAS.Client.Data.DataHelper.Instance.ExecProc(Common.Common.UserInfo.UserName, Common.Common.UserInfo.UserPwd, "PCK_LMSDATAINFO.DELETE_LMS_CONFIRMEDDATA", parms);
            if (!modelProc.RSuccess || modelProc.RStatuCode != 1)
            {
                strErrInfo = string.Format("操作失败!\r\n错误代码:{0}  错误描述:{1}", modelProc.RStatuCode, modelProc.RStatuDesc);
            }
            else
            {
                strErrInfo = modelProc.RParams[modelProc.RParams.Length - 1].Value == null ? string.Empty : modelProc.RParams[modelProc.RParams.Length - 1].Value.ToString().Replace("null", "").Trim();
                if (!string.IsNullOrEmpty(strErrInfo)) return false;
                bResult = true;
            }
            return bResult;
        }

        /// <summary>
        /// 更新平行样已使用状态到采集表
        /// </summary>
        /// <param name="strIDs"></param>
        /// <param name="strErrInfo"></param>
        /// <returns></returns>
        public static bool DoUpdateCollectionDataState(string strIDs, ref string strErrInfo)
        {
            var bResult = false;
            strErrInfo = string.Empty;
            var parms = new[]{    new Titan.DAS.Msg.Common.ProcParam{Name = "P_IDS",Value = strIDs,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRCODE",Value = 0,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRMSG",Value = null,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2),Size=2000}};
            var modelProc = DAS.Client.Data.DataHelper.Instance.ExecProc(Common.Common.UserInfo.UserName, Common.Common.UserInfo.UserPwd, "PCK_LMSDATAINFO.UPDATE_LMS_COLLECTIONSTATE", parms);
            if (!modelProc.RSuccess || modelProc.RStatuCode != 1)
            {
                strErrInfo = string.Format("操作失败!\r\n错误代码:{0}  错误描述:{1}", modelProc.RStatuCode, modelProc.RStatuDesc);
            }
            else
            {
                strErrInfo = modelProc.RParams[modelProc.RParams.Length - 1].Value == null ? string.Empty : modelProc.RParams[modelProc.RParams.Length - 1].Value.ToString().Replace("null", "").Trim();
                if (!string.IsNullOrEmpty(strErrInfo)) return false;
                bResult = true;
            }
            return bResult;
        }

        /// <summary>
        /// 更新化验确认结果已审核状态到确认表
        /// </summary>
        /// <param name="strTestCode"></param>
        /// <param name="strUpdateTime"></param>
        /// <param name="strErrInfo"></param>
        /// <returns></returns>
        public static bool DoUpdateConfirmedDataState(string strTestCode, string strUpdateTime, ref string strErrInfo)
        {
            var bResult = false;
            strErrInfo = string.Empty;
            var parms = new[]{    new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTCODE",Value = strTestCode,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_UPDATETIME",Value = strUpdateTime,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRCODE",Value = 0,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRMSG",Value = null,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2),Size=2000}};
            var modelProc = DAS.Client.Data.DataHelper.Instance.ExecProc(Common.Common.UserInfo.UserName, Common.Common.UserInfo.UserPwd, "PCK_LMSDATAINFO.UPDATE_LMS_CONFIRMEDSTATE", parms);
            if (!modelProc.RSuccess || modelProc.RStatuCode != 1)
            {
                strErrInfo = string.Format("操作失败!\r\n错误代码:{0}  错误描述:{1}", modelProc.RStatuCode, modelProc.RStatuDesc);
            }
            else
            {
                strErrInfo = modelProc.RParams[modelProc.RParams.Length - 1].Value == null ? string.Empty : modelProc.RParams[modelProc.RParams.Length - 1].Value.ToString().Replace("null", "").Trim();
                if (!string.IsNullOrEmpty(strErrInfo)) return false;
                bResult = true;
            }
            return bResult;
        }

        /// <summary>
        /// 化验班长审核通过，提交审批流程
        /// </summary>
        /// <param name="businessType"></param>
        /// <param name="strTestCode"></param>
        /// <param name="strErrInfo"></param>
        /// <returns></returns>
        public static bool DoInsertAutiditAssay(BusinessType businessType, string strTestCode, ref string strErrInfo)
        {
            var bResult = false;
            strErrInfo = string.Empty;
            var parms = new[]{                 new Titan.DAS.Msg.Common.ProcParam{Name = "P_TESTCODE",Value = strTestCode,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_BUSINESSTYPE",Value = (int)businessType,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_USERID",Value = Common.Common.UserInfo.UserId,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRCODE",Value = 0,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                               new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRMSG",Value = null,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2),Size=2000}};
            var modelProc = DAS.Client.Data.DataHelper.Instance.ExecProc(Common.Common.UserInfo.UserName, Common.Common.UserInfo.UserPwd, "PCK_LMSDATAINFO.INSERT_LMS_AUDITDATA", parms);
            if (!modelProc.RSuccess || modelProc.RStatuCode != 1)
            {
                strErrInfo = string.Format("操作失败!\r\n错误代码:{0}  错误描述:{1}", modelProc.RStatuCode, modelProc.RStatuDesc);
            }
            else
            {
                strErrInfo = modelProc.RParams[modelProc.RParams.Length - 1].Value == null ? string.Empty : modelProc.RParams[modelProc.RParams.Length - 1].Value.ToString().Replace("null", "").Trim();
                if (!string.IsNullOrEmpty(strErrInfo)) return false;
                bResult = true;
            }
            return bResult;
        }

    }
}
